package com.game.units {
	import com.game.data.Data;
	
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.geom.Matrix;
	public class TowerGraphics {
		private static const SLICE_SPEED:Number = 18;
		private static const ANGLE_ADJ:Number = (1 / 180) * Math.PI;
		public var size:int = Data.STEP;
		public var padding:int = Data.IMAGE_PADDING;
		private var _graphics:Graphics;
		private var _bmp:BitmapData = new BitmapData(size + padding, size + padding, true, 0);
		private var _animBmp:BitmapData = new BitmapData(size + padding, size + padding, true, 0);
		private var _matrix:Matrix = new Matrix();
		private var _dir:int = 0;
		private var _duration:Number = 0;
		private var _targetAngle:Number = 0;
		private var _rotation:Number = 0;
		private var _turning:Boolean = false;
		private var _type:int=0;
		public var haltTurning:Boolean;
		public function TowerGraphics(graphics:Graphics) {
			_graphics = graphics;
			_graphics.beginBitmapFill(_bmp, new Matrix(1, 0, 0, 1, -(size + padding) / 2, -(size + padding) / 2), false, true);
			_graphics.drawRect(-(size + padding) / 2, -(size + padding) / 2, size + padding, size + padding);
			_graphics.endFill();
		}
		public function init(type:int):void {
			_type=type;
			_targetAngle = _rotation =0;
			_animBmp = Data.getInstance().weaponAnimArray[_type][0];
			redraw(true);
		}
		public function upgrade(value:int):void {
			_animBmp = Data.getInstance().weaponAnimArray[_type][value];
			redraw(true)
		}
		public function move():void {
			
			_duration -= ANGLE_ADJ;
			if (_duration > 0) {
				_turning=true;
				_rotation += SLICE_SPEED * ANGLE_ADJ * _dir;
			} else {
				_turning=false;
				_rotation = _targetAngle;
			}
		}
		public function redraw(force:Boolean=false):void {
			if (force || int(_rotation * 100) != int(Math.acos(_matrix.a) * 100)) {
				_bmp.unlock();
				_matrix.identity();
				_matrix.translate(-(size + this.padding )/2,-(size + this.padding )/2);
				_matrix.rotate(_rotation);
				_matrix.translate((size+padding)/2,(size+padding)/2);
				_bmp.fillRect(_bmp.rect, 0x0);
				_bmp.draw(_animBmp, _matrix, null, null, null, true);
				_bmp.lock();
			}
		}
		public function turn(ang:Number):void {
			_targetAngle = ang;
			var dist:Number = _rotation - ang;
			if (dist >= 0) {
				if (dist > Math.PI) {
					_dir = 1;
					dist -= Math.PI * 2;
				} else {
					_dir = -1;
				}
			} else {
				if (dist < -Math.PI) {
					_dir = -1;
					dist += Math.PI * 2;
				} else {
					_dir = 1;
				}
			}
			//Math.abs replacement  Math.abs(dist / SLICE_SPEED);
			_duration =(dist / SLICE_SPEED) < 0 ? -(dist / SLICE_SPEED) : (dist / SLICE_SPEED);
		}
		public function get turning():Boolean {
			return _turning;
		}
		public function set turning(value:Boolean):void {
			_turning = value;
		}
		public function get rotation():Number {
			return _rotation;
		}
		public function set rotation(value:Number):void {
			_rotation = value;
		}
		
	}
}